<?php
/**
 * Application User List
 * This screen has been copied from Orix to insure security
 * separation
 * It will display the application cloud user list
 * 
 * @author Xavier VILAIN
 * @package Hive\Admin
 * @license #license-hive#
 */

use Fox\Ddic\DataElement\Behavior;
use Fox\Html\DropList;
use Fox\Behaviors;
use Fox\Html\Screen;
use Fox\Html\Input;
use Fox\Dao\SqlFragment;
use Fox\Authorization;
use Fox\Generator;

$xview = Xview::instance();
$id = $xview->application()->getId();
// Customizing
$customizing = Hive\Admin\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'admin','act'=>'users','sac'=>'list'));

if(!Authorization\Manager::check('hive-users-list')) {
	echo $xview->accessDenied(Msg(Hive::BUNDLE,Hive::ERR_ADMIN_ACCES_DENIED),null,true);
	exit;
}
// Load Application
$finder = new \Fox\Authorization\Profile();
foreach($xview->application()->profiles() as $profile) {
	$profiles[$profile->proid] = $profile;
}
$screen = new Screen('sUsers','search',null,true);
$screen->responseTarget('#searchResult');
$screen->action(Generator::url('admin','users','list'));
$screen->mainTitle(Msg(Hive::BUNDLE,Hive::USERS_LIST));
$screen->block()->add(new Input(array('first_name','fname')))
					->useLike()->parentObject()
				->add(new Input(array('last_name','lname')))->useLike()->parentObject()
				->add(new Input('email'))->useLike()->size(Input::SIZE_XLARGE)->parentObject()
				->add(new DropList(array('pronm','proid'),true),2)
					->options($profiles,Generator::useAttribute('proid'),Generator::call('label'))
					->excluded();


if($screen->submitted()) {
	$submit = true;
	$fragments = $screen->fragments();
	if(!is_array($fragments)) {
		$fragments = array($fragments);
	}
	$fragments[] = new SqlFragment('cloudname = ?',array(User::current()->cloudname));
	$fragment = SqlFragment::combine($fragments);
	$finder = Fox\Dao::finder(User::className())
				->where($fragment->where(),$fragment->parameters(),$fragment->types())
				->follow(User::LINK_APPLICATIONS)
					->where('webid = ?',array($id));
	// Application Filter
	$proid = $screen->item('proid')->get();
	if(!empty($proid)) {
		$finder->where('proid = ?',array($proid));
	}			
	$users = $finder->find();
} else {
	$submit = false;
	// Load Data
	$users = Fox\Dao::finder(User::className())
				->where('cloudname = ?',array(User::current()->cloudname))
					->follow(User::LINK_APPLICATIONS)
						->where('webid = ?',array($id))
						->find();
}
// Grid
$grid = new Fox\Grid('users');
$grid->bootstrapGrid()->fieldcat(array('email','fname','lname','proid','activ'));
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->setGenerators(array(
	'proid' => Fox\Generator::useFunction(function ($data,$key) use($profiles) {
		$proid = $data->getProfile();
		if(!empty($proid) && isset($profiles[$proid])) {
			return $profiles[$proid]->label();
		}
	})
));
$grid->fieldcat()->behaviors(Behaviors::action(
	Generator::url('admin','users','update',array('idusr' => Generator::useAttribute('idusr'))),
	false,
	null,
	null,
	false,
	'GET',
	null,
	null,
	'contentCol'
));

$grid->initialize($users,true);
if(!$submit) {
	echo $screen->display();
	echo '<div id="searchResult">'.$grid->display().'</div>';
} else {
	echo $grid->display();
}